package com.xuzz.study.io.netty.rpc.provicer;

import com.alibaba.fastjson.JSON;
import com.xuzz.study.io.netty.rpc.base.RpcRequest;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

/**
 * @author zhize.xu
 * @version 1.0
 * @ClassName:ServiceHandler
 * @Description:
 * @date 2018/11/27 14:54
 */
public class ServiceHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        //在这里可以判断请求规则
//        RpcRequest request = (RpcRequest)msg;

        String reqeustStr = (String)msg;
        RpcRequest request = JSON.toJavaObject(JSON.parseObject(reqeustStr),RpcRequest.class);
        String result ="";
        if(request.getClassName().endsWith("HelloService"))
        {
            result = new HelloServiceImpl().hello(request.getParams()[0].toString());
        }
        else
        {
            result="can not find class";
        }

        ctx.writeAndFlush(result);
    }
}
